#!/usr/bin/perl

if (@ARGV != 1)
{
	print "usage: $0 filename\n";
	exit 0;
}

my $table = {};
my $file = $ARGV[0];

my $before_ticket_str = "before_ticket_num";
my $after_ticket_str = "after_ticket_num";
my $max_ratio_str = "max_ratio";
my $min_ratio_str = "min_ratio";
my $multi_num_str = "multi_num";
my $all_num_str = "all_num";

=log-format
2012-02-14 22:18:43.681 [INFO] c05 [3280:1139198272] ticket_fifo_server.cpp:525:handle_queue_msg filename:mongodb://buy_201202/data/ToTo_20120214221541_881306, issplited:falsned:trueplit:false,multisize:65,singct:47,lottery:ToTo_P, CRatio:45.901639%, before_ticket_num:61, after_ticket_num:28!
=cut

open(FH, "< $file") or die "Couldn't open file[$file] for reading";

while(<FH>)
{
	next unless ($_ =~ /lottery:(.*), CRatio:(.*)%, before_ticket_num:(\d+), after_ticket_num:(\d+)!/);

#print "$1;$2;$3;$4\n";

	if (defined($table->{$1}))
	{
		$table->{$1}->{$before_ticket_str} += $3;
		$table->{$1}->{$after_ticket_str} += $4;

		if ($table->{$1}->{$max_ratio_str} < $2)
		{
			$table->{$1}->{$max_ratio_str} = $2;
		}

		if ($table->{$1}->{$min_ratio_str} > $2)
		{
			$table->{$1}->{$min_ratio_str} = $2;
		}

		$table->{$1}->{$all_num_str} += 1;

		if ($2 != 100)
		{
			$table->{$1}->{$multi_num_str} += 1;
		}
	}
	else
	{
		$table->{$1}->{$before_ticket_str} = $3;
		$table->{$1}->{$after_ticket_str} = $4;
		$table->{$1}->{$max_ratio_str} = $2;
		$table->{$1}->{$min_ratio_str} = $2;
		$table->{$1}->{$all_num_str} = 1;

		if ($2 != 100)
		{
			$table->{$1}->{$multi_num_str} = 1;
		}
		else
		{
			$table->{$1}->{$multi_num_str} = 0;
		}
	}
}

my $str = "   ";

while (($key, $value) = each(%$table) )
{
	print "$key---------\n";
	print "$all_num_str:".$value->{$all_num_str}."$str";

	print "$multi_num_str:".$value->{$multi_num_str}."$str";

	print "$before_ticket_str:".$value->{$before_ticket_str}."$str";

	print "$after_ticket_str:".$value->{$after_ticket_str}."$str\n";

	$average_ratio = $value->{$after_ticket_str}/$value->{$before_ticket_str}*100;
	print "average_ratio:$average_ratio $str";

	print "$max_ratio_str:".$value->{$max_ratio_str}."$str";

	print "$min_ratio_str:".$value->{$min_ratio_str}."$str";

	print "\n\n";
}

exit 0;
